package com.hlhj.funlike_business.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.bean.BitmapEvent;
import com.hlhj.funlike_business.bean.BitmapEvent1;
import com.hlhj.funlike_business.bean.BitmapEvent2;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CameraActivity extends AppCompatActivity {
    @BindView(R.id.jcameraview)
    JCameraView jcameraview;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camear);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }

        type = getIntent().getStringExtra("type");
        initData();
        takePhoto();
    }

    protected void initData() {
        if (ContextCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {

        }
    }

    public void takePhoto() {
        jcameraview.setTip("拍照");
        jcameraview.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE);
        jcameraview.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
                if (type.equals("1")){
                    BitmapEvent1 bitmapEvent = new BitmapEvent1(bitmap);
                    Log.e("JCameraView", "bitmap = " + bitmapEvent.toString());
                    EventBus.getDefault().postSticky(bitmapEvent);
                }else if (type.equals("2")){
                    BitmapEvent bitmapEvent = new BitmapEvent(bitmap);
                    Log.e("JCameraView", "bitmap = " + bitmapEvent.toString());
                    EventBus.getDefault().postSticky(bitmapEvent);
                }else if (type.equals("3")){
                    BitmapEvent2 bitmapEvent = new BitmapEvent2(bitmap);
                    Log.e("JCameraView", "bitmap = " + bitmapEvent.toString());
                    EventBus.getDefault().postSticky(bitmapEvent);
                }
                finish();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
            }
        });
        //左边按钮点击事件
        jcameraview.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                CameraActivity.this.finish();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                //申请成功，可以拍照
                takePhoto();
            } else {
                Toast.makeText(this, "相机权限未打开", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}